<?php
namespace wxpayfood;

require_once 'lib/WxPay.Api.php';
class PayFood
{
    public function getOrder($arr)
    {
        $input = new \WxPayUnifiedOrder();
        $input->SetTotal_fee($arr['money']);
        $input->SetBody("test");
        $input->SetOut_trade_no(\WxPayConfig::MCHID . date("YmdHis"));
        $input->SetNotify_url("https://cs.rongbaiwei.com/index.php/restaurant/pay/payInfo");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($arr['openid']);
        $order = \WxPayApi::unifiedOrder($input);
        return $order;
    }
    public function GetJsApiParameters($UnifiedOrderResult)
    {
        if (!array_key_exists("appid", $UnifiedOrderResult) || !array_key_exists("prepay_id", $UnifiedOrderResult) || $UnifiedOrderResult['prepay_id'] == "") {
            throw new \WxPayException("参数错误");
        }
        $jsapi = new \WxPayJsApiPay();
        $jsapi->SetAppid($UnifiedOrderResult["appid"]);
        $timeStamp = time();
        $jsapi->SetTimeStamp("$timeStamp");
        $jsapi->SetNonceStr(\WxPayApi::getNonceStr());
        $jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
        $jsapi->SetSignType("MD5");
        $jsapi->SetPaySign($jsapi->MakeSign());
        $parameters = json_encode($jsapi->GetValues());
        return $parameters;
    }
}
